<?php
/**
* @version		1.0: view.html.php marum56
* @package		Joomla
*/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

jimport( 'joomla.application.component.view');

/**
 * HTML View class for the Users component
 *
 * @static
 * @package	Joomla
 * @subpackage	Producenci
 * @since 1.0
 */
class ProducenciViewProducenci extends JView
{
	function display($tpl = null)
	{
		global $mainframe, $option;

		$db =& JFactory::getDBO();
		
		/*
		 * Zapis/odczyt stanu strony w obiekcie klasy JRegistry,
		 * który znajduje sie w tablicy sesji.
		 */
		// Filter na wybrana kolumne
		$filter_order 		= $mainframe->getUserStateFromRequest( "$option.filter_order", 'filter_order', 'a.name', 'cmd' );
		// Filter na sposob sortowania: rosnacy lub malejacy
		$filter_order_Dir	= $mainframe->getUserStateFromRequest( "$option.filter_order_Dir", 'filter_order_Dir', '', 'word' );
		$filter_del		= $mainframe->getUserStateFromRequest( "$option.filter_del", 'filter_del', 0, 'int' );
		$search			= $mainframe->getUserStateFromRequest( "$option.search", 'search', '', 'string' );
		if (strpos($search, '"') !== false) {
			$search = str_replace(array('=', '<'), '', $search);
		}
		$search = JString::strtolower($search);
		// Limit ilosci wyswietlanych wierszy na stronie
		$limit	= $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
		// Limit poczatkowy tzn. od ktorego wiersza tabeli sql rozpoczynamy wyswietlanie na stronie
		$limitstart = $mainframe->getUserStateFromRequest( $option.'.limitstart', 'limitstart', 0, 'int' );


		$where = array();
		if (isset( $search ) && $search != '') {
			$searchEscaped = $db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );
			$where[] = 'a.name LIKE '.$searchEscaped;
		}

		// Zapewnienie ze filter_order ma prawidlowa wartosc.
		if (!in_array($filter_order, array('a.name'))) {
			$filter_order = 'a.name';
		}
		// Zapewnienie ze filter_order_Dir ma prawidlowa wartosc.
		if (!in_array(strtoupper($filter_order_Dir), array('ASC', 'DESC'))) {
			$filter_order_Dir = '';
		}
		$orderby = ' ORDER BY '. $filter_order .' '. $filter_order_Dir;
		
		// Wyszukanie najmlodszego rekordu  
		$where[] = 'a.registerDate = (SELECT max(registerDate) 
		                              FROM #__producenci
		                              WHERE nrp = a.nrp)';
		//Filtrowanie po usunietych
		if ( $filter_del == 0) {
			$where[] = 'a.del = 0';
		}
		else if ($filter_del == 1) {
			$where[] = 'a.del = 1';
		}

		$where = ( count( $where ) ? ' WHERE (' . implode( ') AND (', $where ) . ')' : '' );

		/*
		 * Ile rekordow do wyswietlenia.
		 */
		$query = 'SELECT COUNT(a.id)'
		. ' FROM #__producenci AS a'
		. $where
		;
		$db->setQuery( $query );
		$total = $db->loadResult();
		jimport('joomla.html.pagination');
		$pagination = new JPagination( $total, $limitstart, $limit );


		/*
		 * Tabela z producentami.
		 */
		$query = 'SELECT a.*'
			. ' FROM #__producenci AS a'
			. $where
			. ' GROUP BY a.id'
			. $orderby
		;
		$db->setQuery( $query, $pagination->limitstart, $pagination->limit );
		$rows = $db->loadObjectList();

		// Lista selekt usunietych
		$lists['del'] = msSelect::Arch($filter_del, 'Aktualni producenci', 'Archiwalni');

		// table ordering
		$lists['order_Dir']	= $filter_order_Dir;
		$lists['order']		= $filter_order;

		// search filter
		$lists['search'] = $search;

		$this->assignRef('lists', $lists);
		$this->assignRef('items', $rows);
		$this->assignRef('pagination', $pagination);

		parent::display($tpl);
	}
}
